package business.application.test_bed;

import java.text.DecimalFormat;
import java.util.Iterator;

import org.apache.http.HttpStatus;
import org.apache.http.nio.entity.NStringEntity;
import org.kth.dks.JDHT;

import business.search_eng.Document;
import business.search_eng.PostingList;
import business.search_eng.Querier;
import business.search_eng.ResultList;
import business.search_eng.ResultListItem;
import business.search_eng.PostingListItem;

public class DBClient implements Runnable{
	/**
	 * @param args contains a reference to an existing node in a DHT 
	 */
	
	private JDHT dht;
	private static final String someQuery[] = {"music","home made","green","far travels","new film","famous movie stars","disney cartoon","college campus","research","TV singer"};
	public DBClient(JDHT ref) {
		dht = ref;
	}
	
	public static int queries = 5;
	public static long waittime = 5000; //in ms
	public long max;
	public long min;
	public double avg;
	public double std;
	
	@Override
	public void run() {
		max=Integer.MIN_VALUE; min=Integer.MAX_VALUE;avg=0;std = 0;
		
		for(int i=0;i<queries;i++) {
			//System.out.println(" quering "+this.someQuery[i]);
			Querier system = new Querier(this.dht);
            long ini = System.currentTimeMillis();
            
            int index = Math.round((float) Math.random()*someQuery.length);            
            system.query(this.someQuery[index]);
            
            long end = System.currentTimeMillis();
            long diff = (end-ini);
            
            if(diff >max) max=diff;
            if(diff <min) min=diff;
            avg+= diff;
            
            System.out.println("time elapsed in ms "+(end-ini));
            
            while(end+waittime >= System.currentTimeMillis()) {
            	//donothing
            }
		}
		avg=avg/queries;
		//myDHT.close();
	}
	
	public static void test(String ref) {
		try {
			// Start an instance listening on port 5550
			// Connect to an existing node running a DHT at args[0]
			JDHT myDHT = new JDHT(5551, ref);
			String someQuery[] = {"music","far travels","famous movie stars"};
			
			for(int i=0;i<someQuery.length;i++) {
				System.out.println(" quering "+someQuery[i]);
				Querier system = new Querier(myDHT);
	            long ini = System.currentTimeMillis();
	            ResultList result = system.query(someQuery[i]);//.replace("%20", " "));
	            long end = System.currentTimeMillis();
	            System.out.println("time elapsed in ms "+(end-ini));
			}
			myDHT.close();
			
		} catch (Exception e) { e.printStackTrace(); }
	}
	
	public static void main(String[] args) {
		if (args.length!=1) System.exit(0); 
		try {
			// Start an instance listening on port 5550
			// Connect to an existing node running a DHT at args[0]
			JDHT myDHT = new JDHT(5551, args[0]);
			String someQuery="music+world";
			/*
			String someQuery[] = {"music","far travels","famous movie stars"};
			//Integer someKey =55;
			
			for(int i=0;i<someQuery.length;i++) {
				System.out.println(" quering "+someQuery[i]);
				Querier system = new Querier(myDHT);
	            long ini = System.currentTimeMillis();
	            ResultList result = system.query(someQuery[i]);//.replace("%20", " "));
	            long end = System.currentTimeMillis();
	            System.out.println("time elapsed in ms "+(end-ini));
			}
            */
			
			Querier system = new Querier(myDHT);
			
			System.out.println(" quering "+someQuery);

            //PostingList result = system.query(someQuery.replace("+", " "));
            ResultList result = system.query(someQuery.replace("+", " "));

			
			int sizeOfWord =  result.documents.size();
			System.out.println(" done: "+sizeOfWord+ " docs foundt");
			int numOfDocs = 1024;
			
			Iterator<ResultListItem> PLIterator = result.documents.iterator();
			while(PLIterator.hasNext()) {
				ResultListItem doc = PLIterator.next();
				//Document aDocument = (Document) myDHT.get(doc.documentId);
				
		        DecimalFormat df = new DecimalFormat("#.##");
				//System.out.println(aDocument.showData()+"\n"+ df.format(doc.score));
				System.out.println(doc.documentId+"\t"+ df.format(doc.score));
				
			}
		
			myDHT.close();
			
		} catch (Exception e) { e.printStackTrace(); }
		
	}


}
